﻿#include "GUIPickWidget.h"
#include "ui_GUIPickWidget.h"

#include "PickDataProvider/GUIPickInfo.h"
#include "PickDataProvider/PickedData.h"
#include "PickDataProvider/PickedDataProvider.h"

#include <QButtonGroup>

namespace GUI
{
    GUIPickWidget::GUIPickWidget()
    {
        _ui = new Ui::GUIPickWidget();
        _ui->setupUi(this);

        _radioButtonGroup = new QButtonGroup(this);
        _radioButtonGroup->addButton(_ui->radioButton_Geometry, (int)GUIDialogPickType::Geometry);
        _radioButtonGroup->addButton(_ui->radioButton_SubSysteam, (int)GUIDialogPickType::SubSysteam);
        _radioButtonGroup->addButton(_ui->radioButton_Load, (int)GUIDialogPickType::Load);
        _radioButtonGroup->addButton(_ui->radioButton_Junction, (int)GUIDialogPickType::Junction);
        _ui->radioButton_Geometry->setChecked(true);
        slotRadioButtonClicked((int)GUIDialogPickType::Geometry);

        connect(_radioButtonGroup, SIGNAL(buttonClicked(int)), this, SLOT(slotRadioButtonClicked(int)));
    }

    GUIPickWidget::~GUIPickWidget()
    {
        //清空拾取状态
        resetPickToNone();

        if (_ui) {
            delete _ui;
            _ui = nullptr;
        }

        if (_radioButtonGroup) {
            delete _radioButtonGroup;
            _radioButtonGroup = nullptr;
        }
    }

    GUIDialogPickType GUIPickWidget::getCurrentPickType()
    {
        return (GUIDialogPickType)_radioButtonGroup->checkedId();
    }

    QList<int> GUIPickWidget::getPickValue()
    {
        QList<int> result = {};
        GraphData::PickedDataProvider* provider = GraphData::PickedDataProvider::getInstance();
        if (provider == nullptr)return result;
        QList<GraphData::PickedData*> pickList = provider->getPickedList();
        GUIDialogPickType type = getCurrentPickType();
        switch (type){
        case GUI::GUIDialogPickType::Geometry:
            for (GraphData::PickedData* pick : pickList) {
                if (pick == nullptr)continue;
                result.append(pick->getPickedIds());
            }
            break;
        case GUI::GUIDialogPickType::SubSysteam:
        case GUI::GUIDialogPickType::Load:
        case GUI::GUIDialogPickType::Junction:
            for (GraphData::PickedData* pick : pickList) {
                if (pick == nullptr)continue;
                result.append(pick->getPickedDataObjId());
            }
            break;
        }

        return result;
    }

    void GUIPickWidget::resetPickToNone()
    {
        switchPickModel(GUIDialogPickType::None);
    }

    void GUIPickWidget::resetPickToCurrentSelect()
    {
        switchPickModel((GUIDialogPickType)_radioButtonGroup->checkedId());
    }

    void GUIPickWidget::slotRadioButtonClicked(int id)
    {
        resetPickToNone();
        resetPickToCurrentSelect();
    }

    void GUIPickWidget::switchPickModel(GUIDialogPickType type)
    {
        //清空拾取
        GUI::GUIPickInfo::ClearPickInfo();
        //拾取类型对象
        GUI::GUIPickInfoStru pickStr;

        //拾取类型选择
        switch (type) {
        case GUI::GUIDialogPickType::None: {
            pickStr._pickObjType = GUI::GUIPickInfo::PickObjType::POBJNone;
            pickStr._pickMethod = GUI::GUIPickInfo::PickMethod::PMNone;
            break;
        }
        case GUI::GUIDialogPickType::Geometry: {
            pickStr._pickObjType = GUI::GUIPickInfo::PickObjType::POBJGeometry;
            pickStr._pickMethod = GUI::GUIPickInfo::PickMethod::PMIndividually;
            break;
        }
        case GUI::GUIDialogPickType::SubSysteam: {
            pickStr._pickObjType = GUI::GUIPickInfo::PickObjType::POBJSubSystem;
            pickStr._pickMethod = GUI::GUIPickInfo::PickMethod::PMIndividually;
            break;
        }
        case GUI::GUIDialogPickType::Load: {
            pickStr._pickObjType = GUI::GUIPickInfo::PickObjType::POBJLoadAll;
            pickStr._pickMethod = GUI::GUIPickInfo::PickMethod::PMIndividually;
            break;
        }
        case GUI::GUIDialogPickType::Junction: {
            pickStr._pickObjType = GUI::GUIPickInfo::PickObjType::POBJJunctions;
            pickStr._pickMethod = GUI::GUIPickInfo::PickMethod::PMIndividually;
            break;
        }
        }
        GUI::GUIPickInfo::SetPickInfo(pickStr);
    }
}